package com.jintian.smart.kernel.flowable.controller;

import com.jintian.smart.kernel.core.auth.IOperatorProvider;
import com.jintian.smart.kernel.core.domain.Page;
import com.jintian.smart.kernel.core.domain.PageQuery;
import com.jintian.smart.kernel.core.web.RestResultWrapper;
import com.jintian.smart.kernel.flowable.domain.entity.ActHiTaskInst;
import com.jintian.smart.kernel.flowable.domain.query.TaskVoQuery;
import com.jintian.smart.kernel.flowable.mapper.ITaskVoMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程任务相关
 */
@RestResultWrapper
@RestController
@RequestMapping("/flow/query")
@Schema(description = "流程查询")
public class QueryController {
    @Autowired(required = false)
    private ITaskVoMapper taskVoMapper;
    @Autowired(required = false)
    private IOperatorProvider operatorProvider;

//    @Autowired
//    private HistoryService historyService;
//    @Autowired
//    private TaskQueryHandler taskQueryHandler;
//
//    @RequestMapping(value = "/task/taskId ", method = RequestMethod.GET)
//    @ResponseBody
//    @Operation(summary = "任务查询")
//    @Parameters(value = {@Parameter(name = "taskId", in = ParameterIn.DEFAULT, description = "任务ID")})
//    public TaskVO queryTask(String taskId) {
//        Task task = taskQueryHandler.taskId(taskId);
//        TaskVO taskVO = BeanUtils.copyBean(task, TaskVO.class);
//        return taskVO;
//    }
//
//    @RequestMapping(value = "/task/list/userId ", method = RequestMethod.GET)
//    @ResponseBody
//    @Operation(summary = "根据用户ID，查询该用户参与的活动任务列表")
//    @Parameters(value = {@Parameter(name = "userId", in = ParameterIn.DEFAULT, description = "用户ID")})
//    public List<TaskVO> queryUserList(String userId) {
//        List<Task> tasks = taskQueryHandler.taskCandidateOrAssigned(userId);
//        List<TaskVO> list = BeanUtils.copyList(tasks, TaskVO.class);
//        return list;
//    }
//
//    @RequestMapping(value = "/comment ", method = RequestMethod.GET)
//    @ResponseBody
//    @Operation(summary = "查询批注信息")
//    @Parameters(value = {@Parameter(name = "taskId", in = ParameterIn.DEFAULT, description = "任务ID")})
//    public List<Comment> getTaskComments(String taskId) throws Exception {
//        List<Comment> taskComments = taskHandler.getTaskComments(taskId);
//        return taskComments;
//    }
//
//    @RequestMapping(value = "/task/pageList/instanceId", method = RequestMethod.GET)
//    @ResponseBody
//    @Operation(summary = "分页查询任务列表（所有）")
//    @Parameters(value = {@Parameter(name = "processInstanceId", in = ParameterIn.DEFAULT, description = "流程实例ID"),
//            @Parameter(name = "page", in = ParameterIn.DEFAULT, description = "页码"),
//            @Parameter(name = "step", in = ParameterIn.DEFAULT, description = "数量")})
//    public List<HistoricTaskInstance> pageListByInstanceId(String processInstanceId, Integer page, Integer step) {
//        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
//                .processInstanceId(processInstanceId).orderByTaskCreateTime().desc().listPage(page, step);
//        return list;
//    }
//
//    @RequestMapping(value = "/task/list/unclaim", method = RequestMethod.GET)
//    @ResponseBody
//    @Operation(summary = "查询未签收任务列表")
//    @Parameters(value = {@Parameter(name = "userId", in = ParameterIn.DEFAULT, description = "用户ID"),
//            @Parameter(name = "page", in = ParameterIn.DEFAULT, description = "页码"),
//            @Parameter(name = "step", in = ParameterIn.DEFAULT, description = "数量")})
//    public List<TaskVO> unclaim(String userId, Integer page, Integer step) {
//        List<Task> taskList = taskQueryHandler.taskCandidateUser(userId, page, step);
//        List<TaskVO> list = BeanUtils.copyList(taskList, TaskVO.class);
//        return list;
//    }
//
//    @RequestMapping(value = "/task/list/claimed", method = RequestMethod.GET)
//    @ResponseBody
//    @Operation(summary = "查询已签收任务列表")
//    @Parameters(value = {@Parameter(name = "userId", in = ParameterIn.DEFAULT, description = "用户ID")})
//    public List<HistoricTaskInstance> claimed(String userId) {
//        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).list();
//        return list;
//    }
//
//    @RequestMapping(value = "/task/active", method = RequestMethod.GET)
//    @ResponseBody
//    @Operation(summary = "根据实例ID，查询当前活动任务")
//    @Parameters(value = {@Parameter(name = "processInstanceId", in = ParameterIn.DEFAULT, description = "实例ID")})
//    public HistoricTaskInstance query(String processInstanceId) throws Exception {
//        HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery()
//                .processInstanceId(processInstanceId).unfinished().singleResult();
//        return historicTaskInstance;
//    }


    @RequestMapping(value = "/task/list", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "待办任务")
    public Object list(String kind, @Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        if ("waiting".equals(kind)) {
            return taskVoMapper.queryWaitingTask(operatorProvider.currentOperator(), request);
        } else if ("done".equals(kind)) {
            return taskVoMapper.queryDoneTask(operatorProvider.currentOperator(), request);
        } else if ("finish".equals(kind)) {
            return taskVoMapper.queryFinishTask(operatorProvider.currentOperator(), request);
        } else if ("abort".equals(kind)) {
            return taskVoMapper.queryAbortTask(operatorProvider.currentOperator(), request);
        }
        throw new IllegalArgumentException("无效kind参数值:" + kind);
    }

    @RequestMapping(value = "/task/list/waiting", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "待办任务")
    public Object waitingList(@Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        return new Page(request, taskVoMapper.queryWaitingTask(operatorProvider.currentOperator(), request));
    }

    @RequestMapping(value = "/task/list/done", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "已办任务")
    public Object doneList(@Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        return new Page(request, taskVoMapper.queryDoneTask(operatorProvider.currentOperator(), request));
    }

    @RequestMapping(value = "/task/list/finish", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "办结任务")
    public Object finishList(@Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        return new Page(request, taskVoMapper.queryFinishTask(operatorProvider.currentOperator(), request));
    }

    @RequestMapping(value = "/task/list/abort", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "作废任务")
    public Object abortList(@Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        return new Page(request, taskVoMapper.queryAbortTask(operatorProvider.currentOperator(), request));
    }

    // ==========================================统计================================================
    @RequestMapping(value = "/task/statistics/waiting", method = RequestMethod.GET)
    @ResponseBody
    @Operation(summary = "待办任务")
    public List<Map<String, Object>> waitingStatistics() throws Exception {
        return taskVoMapper.statisticsWaitingTask(operatorProvider.currentOperator());
    }

    @RequestMapping(value = "/task/statistics/done", method = RequestMethod.GET)
    @ResponseBody
    @Operation(summary = "已办任务")
    public List<Map<String, Object>> doneStatistics() throws Exception {

        return taskVoMapper.statisticsDoneTask(operatorProvider.currentOperator());
    }

    @RequestMapping(value = "/task/statistics/finish", method = RequestMethod.GET)
    @ResponseBody
    @Operation(summary = "办结任务")
    public List<Map<String, Object>> finishStatistics() throws Exception {
        return taskVoMapper.statisticsFinishTask(operatorProvider.currentOperator());
    }

    @RequestMapping(value = "/task/statistics/abort", method = RequestMethod.GET)
    @ResponseBody
    @Operation(summary = "作废任务")
    public List<Map<String, Object>> abortStatistics() throws Exception {
        return taskVoMapper.statisticsAbortTask(operatorProvider.currentOperator());
    }

    @RequestMapping(value = "/task/statistics", method = RequestMethod.GET)
    @ResponseBody
    @Operation(summary = "任务统计")
    public List<Map<String, Object>> statistics(String kind) throws Exception {
        if ("waiting".equals(kind)) {
            return taskVoMapper.statisticsWaitingTask(operatorProvider.currentOperator());
        } else if ("done".equals(kind)) {
            return taskVoMapper.statisticsDoneTask(operatorProvider.currentOperator());
        } else if ("finish".equals(kind)) {
            return taskVoMapper.statisticsFinishTask(operatorProvider.currentOperator());
        } else if ("abort".equals(kind)) {
            return taskVoMapper.statisticsAbortTask(operatorProvider.currentOperator());
        }
        throw new IllegalArgumentException("无效kind参数值:" + kind);
    }
}
